package com.sec.android.app.myfiles.listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.filelist.FileListFragment;
import com.sec.android.app.myfiles.fragment.securefilelist.SecureFileListFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.PreferenceUtils;

public class LocalBroadcastReceiver extends AbsBroadcastReceiverImp {

    //TODO HYL FileListFragment 改成了 --> AbsMyFilesFragment, 为了支持SecureFileListFragment
    private AbsMyFilesFragment mFragment;

    public LocalBroadcastReceiver(Context context, FileListFragment fragment, ListenerMgr.LifeCycle registerTime, ListenerMgr.LifeCycle unregisterTime) {
        super(context, registerTime, unregisterTime);
        mFragment = fragment;
    }

    public LocalBroadcastReceiver(Context context, SecureFileListFragment fragment, ListenerMgr.LifeCycle registerTime, ListenerMgr.LifeCycle unregisterTime) {
        super(context, registerTime, unregisterTime);
        mFragment = fragment;
    }

    @Override
    IntentFilter getIntentFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(AppConstants.MYFILES_ACTION_VIEW_AS_CHANGED);
        filter.addAction(AppConstants.MYFILES_ACTION_SORT_BY_CHANGED);
        filter.addAction(AppConstants.MYFILES_ACTION_SHOW_HIDDEN_FILES_CHANGED);
        return filter;
    }

    @Override
    BroadcastReceiver getBroadcastReceiver() {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                NavigationInfo naviInfo = mFragment.getNavigationInfo();
                if (naviInfo != null) {
                    FileRecord record = naviInfo.getCurRecord();
                    if (record != null) {
                        FileRecord.StorageType storageType = record.getStorageType();
                        if (storageType != FileRecord.StorageType.Downloads && storageType != FileRecord.StorageType.OptimizeStorage) {
                            int senderProcessId = intent.getIntExtra(AppConstants.EXTRA_PROCESS_ID, -1);
                            int recvProcessId = mFragment.getProcessId();
                            if (senderProcessId != -1 && recvProcessId != senderProcessId) {
                                PreferenceUtils.ViewAsKey viewAsKey = PreferenceUtils.getViewAsKey(record);
                                if ((mFragment instanceof FileListFragment) || (mFragment instanceof SecureFileListFragment)) {
                                    int viewType = 0;
                                    if (mFragment instanceof FileListFragment) {
                                        viewType = ((FileListFragment) mFragment).getViewType();
                                    } else if (mFragment instanceof SecureFileListFragment) {
                                        viewType = ((SecureFileListFragment) mFragment).getViewType();
                                    }
                                    if (PreferenceUtils.getViewAs(mContext, viewAsKey) != viewType
                                            || AppConstants.MYFILES_ACTION_SORT_BY_CHANGED.equals(intent.getAction())
                                            || AppConstants.MYFILES_ACTION_SHOW_HIDDEN_FILES_CHANGED.equals(intent.getAction())) {
                                        if (mFragment.isSelectActionMode() || naviInfo.isCompressPreview()) {
                                            mFragment.setNeedRefreshViewAs(true);
                                        } else {
                                            NavigationManager.getInstance(recvProcessId).refreshNotify();
                                        }
                                    }
                                }

                            }
                            if (storageType != FileRecord.StorageType.Cloud && AppConstants.MYFILES_ACTION_SHOW_HIDDEN_FILES_CHANGED.equals(intent.getAction())) {
                                if (FileUtils.isUnderHiddenFolder(record.getFullPath())) {
                                    MyFilesFacade.goHome(mFragment.getProcessId(), null, mContext);
                                }
                            }
                        }
                    }
                }
            }
        };
    }

    @Override
    public void registerListener() {
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, mIntentFilter);
    }

    @Override
    public void unregisterListener() {
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
    }
}
